<?php

include_once $_SERVER['DOCUMENT_ROOT']."/controller/connect.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Template.php";
/**
 * Created by PhpStorm.
 * User: lwh
 * Date: 2017/7/20
 * Time: 16:46
 */
class TemplateDAO{
    const  TABLE_TEMPLATE = "template";
    const  FIELD_ID = "templateId";
    const  FIELD_USER_ID = "userId";
    const  FIELD_NAME = "tempName";
    const  FIELD_URI = "uri";
    const  FIELD_CREATE_TIME = "create_time";
    const  FIELD_TOTAL_STAGE_NUM = "stageNum";
    const  FIELD_HEAT = "heat";
    const  FIELD_DESCRIPTION = "tempDesc";

    public $conn;

    function __construct(){
        $this->conn = new db();
    }

    //插入模板
    public function insert($template){

    }

    //删除模板
    public function delete($templateId){

    }

    //查询模板
    public function query($templateId){
        $sql = "select * from template where ".self::FIELD_ID." = ".$templateId;
        $db = $this->conn->getDBConn();
        $row = $db->query($sql)->fetch(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if($row){
            $template = $this->loadDataFromResult($row);
            return $template;
        }
        else
            return null;
    }

    //更新模板
    public function update($template){

    }

    //从结果集中构建出template对象
    private function loadDataFromResult($row){
        $template = new Template();
        $template->setId($row[self::FIELD_ID]);
        $template->setCreateTime($row[self::FIELD_CREATE_TIME]);
        $template->setDescription($row[self::FIELD_DESCRIPTION]);
        $template->setHeat($row[self::FIELD_HEAT]);
        $template->setName($row[self::FIELD_NAME]);
        $template->setTotalStageNum($row[self::FIELD_TOTAL_STAGE_NUM]);
        $template->setUri($row[self::FIELD_URI]);
        $template->setUserId($row[self::FIELD_USER_ID]);
        return $template;
    }
}
